from enum import Enum

from app.lib.model import Model
from tortoise import fields


class TagTypeEnum(Enum):
    article = "article"
    user = "user"


class Tag(Model):
    class Meta:
        table = "Tag"

    auto_id = fields.IntField(pk=True)
    tag_name = fields.CharField(max_length=100, unique=True)
    tag_type = fields.CharEnumField(TagTypeEnum)
    description = fields.CharField(max_length=255)
    create_time = fields.DatetimeField(auto_now_add=True)
    update_time = fields.DatetimeField(auto_now=True)

    users: fields.ManyToManyRelation["User"]  # noqa: F821

    @classmethod
    async def create_tag(
        cls, tag_name: str, tag_type: TagTypeEnum, description: str = ""
    ) -> "Tag":
        return await cls.create(
            tag_name=tag_name, tag_type=tag_type, description=description
        )
